/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland03.data.map.service;

import java.util.*;

import dawnland03.data.map.algorithm.astar.AStarAlgorithm;
import dawnland03.data.map.algorithm.astar.MatrixOperations;
import dawnland03.data.map.algorithm.neighbours.FindNeighboursAlgorithm;
import dawnland03.data.map.algorithm.neighbours.Predicate;
import dawnland03.data.map.io.reader.MatrixXMLReader;
import dawnland03.data.map.io.reader.sax.MatrixXMLSAXReader;
import dawnland03.data.map.model.Matrix;
import dawnland03.data.map.model.MatrixCell;
import dawnland03.model.entity.Entity;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class MatrixService {
    private MatrixXMLReader matrixXMLReader;
    private AStarAlgorithm aStarAlgorithm;
    private FindNeighboursAlgorithm findNeighboursAlgorithm;

    public MatrixService(MatrixOperations matrixOperations) {
        aStarAlgorithm = new AStarAlgorithm(matrixOperations);
        findNeighboursAlgorithm = new FindNeighboursAlgorithm();
    }

    public Matrix readMatrix(String fileName) {
        return getMatrixXMLReader().readMatrix(fileName);
    }

    public List<MatrixCell> getClosestPath(Entity entity, Matrix matrix, int xStart, int yStart, int xFinish, int yFinish) {
        return getAStarAlgorithm().getClosestPath(entity, matrix, xStart, yStart, xFinish, yFinish);
    }

    public Map<Integer, Set<MatrixCell>> findNeighbours(Matrix matrix, int xStart, int yStart, int minimumDistance, int maximumDistance, Predicate predicate, Integer numberOfNeighbours){
        return findNeighboursAlgorithm.findNeighbours(matrix, xStart, yStart, minimumDistance, maximumDistance, predicate, numberOfNeighbours);
    }

    private MatrixXMLReader getMatrixXMLReader() {
        if (matrixXMLReader == null) {
            matrixXMLReader = new MatrixXMLSAXReader();
        }
        return matrixXMLReader;
    }

    private AStarAlgorithm getAStarAlgorithm() {
        return aStarAlgorithm;
    }
}
